<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;

class Video extends Model
{
	use HasDateTimeFormatter;    

    // protected $hidden = ['base64','title','category_id','state','is_recommend'];
    
    public $fillable = [
        'title','face_img','state','category_id','url','is_recommend','shareid','time','base64','play_count','paynum'
    ];
	//局部作用域--是否显示
	public function scopeShow($query){
        return $query->where('state', 1);
    }

	public function category(){
		return $this->belongsTo(VideoCategory::class);
	}

	protected static function boot()
    {
        parent::boot();
        // 监听模型创建事件，在写入数据库之前触发
        static::creating(function ($model) {
            // 随机播放量
            $model->play_count = random(6,'number');
        });
    }

    public function getCTitleAttribute()
    {
        return xor_enc($this->title);
    }
    //追加自定义字段，未解密的标题
    protected $appends = ['c_title'];

    /**
     * 标题访问器
     */
    public function getTitleAttribute($value){
        return xor_enc($value);
    }

    /**
     * 标题修改器
     */
    public function setTitleAttribute($value){
        $this->attributes['title'] = xor_enc($value);
    }

    /**
     * 视频标签
     */
    public function tags(){
        return $this->belongsToMany(Tag::class);
    }
}
